Xbasic

LOOKUP Function

Syntax

Lookup_Value as A = LOOKUP(C tablename,C filter,C result_expression)

Arguments

Lookup_Value

If this is character data, it can be a maximum of 1023 characters long. To return longer strings, use TABLE.EXTERNAL_RECORD_CONTENT_GET().

tablename

The name of a table. Lookup_Table can include a full drive and path specification. If you omit the drive and path, Alpha Anywhere searches the directory of the current table.

filter

Optional. Default = ".T." (All records). A character filter expression that evaluates to a logical value and selects records from the table.

result_expression

The name of a field in the Lookup_Table or a valid expression which may contain one or more fields.

Description

Returns the value of an expression in an external table for first match of filter.

Discussion

LOOKUP() searches an external lookup table ( Lookup_Table ) for the first record matching the Filter, and returns the value in the Lookup_Expression from the matching record. The Filter must return a logical value, either true or false. For example, to choose all of the records in a table, use the logical constant ".T." as a filter. To choose only the records where the STATE field is equal to ME, use the filter "STATE = 'ME'". Note that the entire filter is always in quotation marks, and the character value, ME, is in single quotes. The filter expression could also have been expressed as "STATE=\"ME\"". In this second case, the backslash character is used to escape the double quotes inside the quoted string. If no records satisfy the filter, Alpha Anywhere will return blank values for character and date fields, zero for numeric fields, and false for logical fields.

Example

Assume that a human resources table ( EMPLOYEE ) contains the following records:

EMPLOYEE

NAME

DEPARTMENT

Armour

Marketing

Blackett

Securities Regulation

Carr

Logistics and Transportation

Cash

Custodial Engineering

Spitzer

Finance

The following expression returns the first match in the DEPARTMENT field where NAME is equal to Spitzer:

lookup("EMPLOYEE.DBF", "UT(NAME) = 'SPITZER'", "DEPARTMENT") -> Finance

Note : Spitzer must be contained within single quotes to distinguish it from the double quotes that contain the filter expression.

These examples use the Invoice_items table that come with the sample AlphaSports database.

lookup("invoice_items","product_id = 'p005'","price") -> 3.52
'In this example we use escaped double quotes in the filter expression, rather than single quotes. 
lookup("invoice_items","product_id = \"p005\"","price") ->3.52
'This example shows how to use a variable in the filter expression string.
dim p_id as c 
p_id = "p006" 
dim filterstring as c 
filterstring = "product_id = " + s_quote(p_id) 
lookup("invoice_items",filterstring,"price") -> 353.42

See Also